﻿using DailyApp.WPF.HttpClients;
using DailyApp.WPF.Service;
using DailyApp.WPF.ViewModels;
using DailyApp.WPF.ViewModels.Dialogs;
using DailyApp.WPF.Views;
using DailyApp.WPF.Views.Daillogs;
using DryIoc;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Services.Dialogs;
using System.Configuration;
using System.Data;
using System.Windows;

namespace DailyApp.WPF
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        /// <summary>
        /// 启动窗口
        /// </summary>
        /// <returns></returns>
        protected override Window CreateShell()
        {
            return Container.Resolve<MainWindow>();
        }

        /// <summary>
        /// 依赖注入
        /// </summary>
        /// <param name="containerRegistry"></param>
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            //登录
            containerRegistry.RegisterDialog<LoginUserControl, LoginUserControlViewModel>();

            //请求
            containerRegistry.GetContainer().Register<HttpRestClient>(made: Parameters.Of.Type<string>(serviceKey: "webUrl"));

            //导航页
            containerRegistry.RegisterForNavigation<HomeUC, HomeUCViewModel>();//首页
            containerRegistry.RegisterForNavigation<MemoUC, MemoUCViewModel>();//备忘录
            containerRegistry.RegisterForNavigation<SettingsUC, SettingsUCViewModel>();//设置
            containerRegistry.RegisterForNavigation<WaitUC, WaitUCViewModel>();//待办事项

            //设置左侧导航
            containerRegistry.RegisterForNavigation<PersonalUC, PersonalUCViewModel>();//个性化
            containerRegistry.RegisterForNavigation<AboutUsUC>();//关于更多
            containerRegistry.RegisterForNavigation<SysSetUC>();//系统设置

            //添加待办事项
            containerRegistry.RegisterForNavigation<AddWaitUC,AddWaitUCViewModel>();
            //修改待办事项
            containerRegistry.RegisterForNavigation<EditWaitUC, EditWaitUCViewModel>();

            //添加备忘录
            containerRegistry.RegisterForNavigation<AddMemoUC, AddMemoUCViewModel>();
            //修改备忘录
            containerRegistry.RegisterForNavigation<EditMemoUC, EditMemoUCViewModel>();

            //自定义对话框服务
            containerRegistry.Register<DialogHostService>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        protected override void OnInitialized()
        {
            var dialog = Container.Resolve<IDialogService>();
            dialog.ShowDialog("LoginUserControl", callback =>
            {
                if (callback.Result != ButtonResult.OK)
                {
                    Environment.Exit(0);
                    return;
                }
                var mainVM = Current.MainWindow.DataContext as MainWindowViewModel;
                if (mainVM != null)
                {
                    if (callback.Parameters.ContainsKey("LoginName")){
                        string name = callback.Parameters.GetValue<string>("LoginName");
                        mainVM.SetDefaultNav(name);
                    }
                    
                }

                base.OnInitialized();
            });
        }
    }

}
